<template>
  <div class="app-container">
    <!-- 搜索工作栏 -->
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="机器码" prop="faasSn">
        <el-input v-model="queryParams.faasSn" placeholder="请输入机器码" clearable size="small" @keyup.enter.native="handleQuery"/>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <!-- 操作工具栏 -->
    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button type="primary" plain icon="el-icon-plus" size="mini" @click="handleAdd"
                   v-hasPermi="['mszf:sys-license:create']">新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="warning" plain icon="el-icon-download" size="mini" @click="handleExport"
                   v-hasPermi="['mszf:sys-license:export']">导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <!-- 列表 -->
    <el-table v-loading="loading" :data="list">
      <el-table-column label="主键" align="center" prop="id" v-if="show"/>
      <el-table-column label="租户ID" align="center" prop="lesseeId" v-if="show"/>
      <el-table-column label="机器码" align="center" prop="faasSn" width="280"/>
      <el-table-column label="验证码" align="center" prop="verifySn" />
      <el-table-column label="最大在线人数" align="center" prop="onlineNum" :formatter="onlineNumFormat"/>
      <el-table-column label="验证IP" align="center" prop="ips" :formatter="ipsFormat"/>
      <el-table-column label="创建时间" align="center" prop="createTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.createTime) }}</span>
        </template>
      </el-table-column>
      <el-table-column label="备注" align="center" prop="remark" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button size="mini" type="text" icon="el-icon-download" @click="handleDownload(scope.row)"
                     v-hasPermi="['mszf:sys-license:download']">下载</el-button>
          <el-button size="mini" type="text" icon="el-icon-edit" @click="handleUpdate(scope.row)"
                     v-hasPermi="['mszf:sys-license:update']">修改</el-button>
          <el-button size="mini" type="text" icon="el-icon-delete" @click="handleDelete(scope.row)"
                     v-hasPermi="['mszf:sys-license:delete']">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <!-- 分页组件 -->
    <pagination v-show="total > 0" :total="total" :page.sync="queryParams.pageNo" :limit.sync="queryParams.pageSize"
                @pagination="getList"/>

    <!-- 对话框(添加 / 修改) -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="auto">
        <el-form-item label="机器码" prop="faasSn">
          <el-input v-model="form.faasSn" placeholder="请输入机器码" />
        </el-form-item>
        <el-form-item label="验证码" prop="verifySn">
          <el-input v-model="form.verifySn" placeholder="请输入验证码" />
        </el-form-item>
        <el-form-item label="最大在线人数" prop="onlineNum">
          <el-input v-model="form.onlineNum" placeholder="请输入最大在线人数" />
        </el-form-item>
        <el-form-item label="验证IP" prop="ips">
          <el-input v-model="form.ips" placeholder="请输入验证IP" />
        </el-form-item>
        <el-form-item label="备注" prop="remark">
          <el-input v-model="form.remark" placeholder="请输入备注" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import  {
  createSysLicense,
  updateSysLicense,
  deleteSysLicense,
  getSysLicense,
  getSysLicensePage,
  exportSysLicenseExcel,
  checkVerifySn, downloadSysLicense
} from "@/api/product/license";
import { isShow } from '@/utils/constants'
import {DICT_TYPE, getDictDataLabel} from "@/utils/dict";
import {isNotEmpty} from "@/utils/ruoyi";
export default {
  name: "SysLicense",
  components: {
  },
  data() {
    var that = this;
    var validateVerifySn = (rule, value, callback) => {
      if (!value) {
        return callback(new Error('验证码不能为空'));
      } else {
        debugger
        checkVerifySn({faasSn:that.form.faasSn,verifySn:value}).then(response => {
          if(response.data) {
            callback();
          } else {
            callback(new Error('机器码或者验证码不正确！'));
          }
        })
      }
    };
    return {
      show:isShow,
      // 遮罩层
      loading: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 产品许可列表
      list: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      dateRangeCreateTime: [],
      // 查询参数
      queryParams: {
        pageNo: 1,
        pageSize: 10,
        lesseeId: null,
        faasSn: null,
        verifySn: null,
        onlineNum: null,
        ips: null,
        status: null,
        remark: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        faasSn: [{ required: true,min: 32, max: 32, message: "机器码不能为空", trigger: "blur" }],
        onlineNum: [{ min: 1, max: 100, message: '长度在 1 到 100 之间', trigger: 'blur' }],
        verifySn: [{ required: true,min: 6, max: 6,  trigger: "blur",validator: validateVerifySn }],
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询列表 */
    getList() {
      this.loading = true;
      // 处理查询参数
      let params = {...this.queryParams};
      this.addBeginAndEndTime(params, this.dateRangeCreateTime, 'createTime');
      // 执行查询
      getSysLicensePage(params).then(response => {
        this.list = response.data.list;
        this.total = response.data.total;
        this.loading = false;
      });
    },
    /** 取消按钮 */
    cancel() {
      this.open = false;
      this.reset();
    },
    /** 表单重置 */
    reset() {
      this.form = {
        id: undefined,
        lesseeId: undefined,
        faasSn: undefined,
        verifySn: undefined,
        onlineNum: undefined,
        ips: undefined,
        status: undefined,
        remark: undefined,
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNo = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.dateRangeCreateTime = [];
      this.resetForm("queryForm");
      this.handleQuery();
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加产品许可";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id;
      getSysLicense(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改产品许可";
      });
    },
    handleDownload(row){
      this.downloadFile(row);
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (!valid) {
          return;
        }
        // 修改的提交
        if (this.form.id != null) {
          updateSysLicense(this.form).then(response => {
            this.msgSuccess("修改成功");
            this.open = false;
            this.getList();
          });
          return;
        }
        // 添加的提交
        createSysLicense(this.form).then(response => {
          this.msgSuccess("新增成功");
          this.open = false;
          this.getList();
          debugger
          this.downloadFile(response.data);
        });
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const id = row.id;
      this.$confirm('是否确认删除产品许可编号为"' + id + '"的数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return deleteSysLicense(id);
        }).then(() => {
          this.getList();
          this.msgSuccess("删除成功");
        })
    },
    /** 导出按钮操作 */
    handleExport() {
      // 处理查询参数
      let params = {...this.queryParams};
      params.pageNo = undefined;
      params.pageSize = undefined;
      this.addBeginAndEndTime(params, this.dateRangeCreateTime, 'createTime');
      // 执行导出
      this.$confirm('是否确认导出所有产品许可数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return exportSysLicenseExcel(params);
        }).then(response => {
          this.downloadExcel(response, '产品许可.xls');
        })
    },
    /** 下载文件 */
    downloadFile(item) {
      console.log(item);
      downloadSysLicense(item.id).then(response => {
        this.downloadOctet(response, 'license.lic');
      });
    },
    onlineNumFormat(row, column) {
      if(row.onlineNum === -1 || this.isEmpty(row.onlineNum)){
        return "无限制";
      }
      return row.onlineNum;
    },
    ipsFormat(row, column){
      if(this.isEmpty(row.ips)){
        return "无";
      }
      return row.ips;
    },
  }
};
</script>
